﻿using Marbles.Core.Settings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Configuration;
using System.IO;
using System.Net;
using Marbles.Core.Authentication;

namespace Marbles.Web.Setup
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (File.Exists(Server.MapPath("~/Config.txt"))) { Response.Redirect("/", true); }

            if (!Page.IsPostBack)
            {
                if (!File.Exists(Server.MapPath("~/Web.Config")))
                {
                    SettingsManager.WriteBaseConfiguration();
                    SettingsManager.RegisterHttpHandlers();
                    SettingsManager.RegisterHttpModules();
                }
                LoadData();
            }
        }

        private void LoadData()
        {
            if (SettingsManager.Connection != null)
            {
                cDrpProvider.SelectedValue = SettingsManager.Connection.ProviderName;
                cTxtHost.Text = SettingsManager.Connection.Host;
                cTxtUsername.Text = SettingsManager.Connection.Username;
                cTxtPassword.Text = SettingsManager.Connection.Password;
                cTxtPassword.Attributes["value"] = cTxtPassword.Text;
                cTxtDatabase.Text = SettingsManager.Connection.Schema;
            }

            cTxtResourceDirectoryRoot.Text = SettingsManager.ResourceDirectoryRoot;
            cDrpAssetCompression.SelectedValue = SettingsManager.AssetCompression;
            cDrpAuthenticationProvider.SelectedValue = SettingsManager.AuthenticationProvider;
            cDrpPageCompression.SelectedValue = SettingsManager.PageCompression;
            cDrpPageStatePersister.SelectedValue = SettingsManager.PageStatePersister;
            cTxtGoogleAPIKey.Text = SettingsManager.GoogleAPIKey;
            cTxtUserFormMailAddress.Text = SettingsManager.UserFormMailAddress;
            cTxtTemplateDir.Text = SettingsManager.TemplateFolder;
            cTxtRunTimeRecipient.Text = SettingsManager.RunTimeErrorMailAddress;
            cChkRunTimeError.Checked = SettingsManager.EnableRunTimeErrorMailing;
            cChkShowFullDebugInfoToClient.Checked = SettingsManager.ShowFullDebugInfoToClient;
            cChkDevMode.Checked = SettingsManager.DeveloperMode;
            cTxtModuleInstallationDirectory.Text = SettingsManager.ModuleInstallationDirectory;
            cChkWysiwyg.Checked = SettingsManager.EnableWysiwygEditing;
        }

        protected void cBtnSave_Click(object sender, EventArgs e)
        {
            DataConnectionSettings c = new DataConnectionSettings();
            c.ProviderName = cDrpProvider.SelectedValue;
            c.Host = cTxtHost.Text;
            c.Username = cTxtUsername.Text;
            c.Password = cTxtPassword.Text;
            c.Schema = cTxtDatabase.Text;
            SettingsManager.Connection = c;
            SettingsManager.ModuleInstallationDirectory = cTxtModuleInstallationDirectory.Text;
            SettingsManager.EnableWysiwygEditing = cChkWysiwyg.Checked;
            SettingsManager.ResourceDirectoryRoot = cTxtResourceDirectoryRoot.Text;
            SettingsManager.AssetCompression = cDrpAssetCompression.SelectedValue;
            SettingsManager.AuthenticationProvider = cDrpAuthenticationProvider.SelectedValue;
            SettingsManager.PageCompression = cDrpPageCompression.SelectedValue;
            SettingsManager.PageStatePersister = cDrpPageStatePersister.SelectedValue;
            SettingsManager.UserFormMailAddress = cTxtUserFormMailAddress.Text;
            SettingsManager.TemplateFolder = cTxtTemplateDir.Text;
            SettingsManager.RunTimeErrorMailAddress = cTxtRunTimeRecipient.Text.Trim();
            SettingsManager.EnableRunTimeErrorMailing = cChkRunTimeError.Checked;
            SettingsManager.ShowFullDebugInfoToClient = cChkShowFullDebugInfoToClient.Checked;
            SettingsManager.DeveloperMode = cChkDevMode.Checked;

            Core.Content.Sites.SiteDefinition web = null;


            if (Core.Content.Sites.SiteDefinition.GetSiteCount() == 0)
            {
                web = Core.Content.Sites.SiteDefinition.CreateNew();
                web.SiteName = "Default";
                web.Host = Context.Request.Url.Host;
                web.Title = "Default Site";
                web.Insert();
            }
            else
            {
                web = Core.Content.Sites.SiteDefinition.GetCurrent();
            }


            if ((!string.IsNullOrEmpty(cTxtAdminUsername.Text)) && (!string.IsNullOrEmpty(cTxtAdminPassword.Text)))
            {
                AuthenticationProvider.CreateRootUser(web.SiteId, cTxtAdminUsername.Text, cTxtAdminPassword.Text);
            }

            string filename = Server.MapPath("~/Config.txt");
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            StreamWriter sw = new StreamWriter(filename);
            sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            sw.Flush();
            sw.Close();

            SettingsManager.RestartApplication();
            Response.Redirect("/Default.aspx", true);

        }


    }
}